package org.pidan.basic.multiThread;

/**
 * {@code @description}: 线程等待唤醒
 * {@code @author}: huangyuxin
 * {@code @date}: 2023-12-12
 */
public class DengDaiHuanXing {

    static Object object = new Object();


    /**
     * 等待
     */
    public static class T1 extends Thread {

        @Override
        public void run() {
            synchronized (object) {
                System.out.println(System.currentTimeMillis() + "  T1 start");

                try {
                    System.out.println(System.currentTimeMillis() + "  T1 wait");
                    object.wait();
                } catch (Exception e) {
                    e.printStackTrace();
                }

                System.out.println(System.currentTimeMillis() + "  T1 end");
            }
        }
    }


    /**
     * 唤醒
     */
    public static class T2 extends Thread {
        @Override
        public void run() {
            synchronized (object) {

                System.out.println(System.currentTimeMillis() + "  T2 start");
                object.notify();

                System.out.println(System.currentTimeMillis() + "  T2 end");
                try {
                    Thread.sleep(1000);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * T2调用notify方法之后，T1并不能立即继续执行，而是要等待T2释放objec投递锁之后，T1重新成功获取锁后，才能继续执行。
     */
    public static void main(String[] args) {
        new T1().start();
        new T2().start();
    }


}
